Imports DirectShowLib
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging

Public Class DialogCamera
    Inherits templateFormDialog

    Private cam As captureGIGI.Capture
    Private VIDEODEVICE As Integer = 0
    Const VIDEOWIDTH As Integer = 320
    Const VIDEOHEIGHT As Integer = 240
    Const VIDEOBITSPERPIXEL As Integer = 24
    Private m_ip As IntPtr = IntPtr.Zero
    Dim lstMain As New ListBox
    Public pic As Image
    Public devicename As String

    Private Sub DialogCamera_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        cam.Dispose()
        If Not m_ip.Equals(IntPtr.Zero) Then
            Marshal.FreeCoTaskMem(m_ip)
            m_ip = IntPtr.Zero
        End If
        Timer1.Stop()
        Timer2.Stop()
    End Sub

    Private Sub DialogCamera_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        VIDEODEVICE = My.Settings.DentalCamera 'Session.DentalCamera
        If devicename = "USB PC Camera-168" Then
            btncapture.Visible = False
            cam = New captureGIGI.Capture(VIDEODEVICE, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, PictureBox1)
            Timer1.Start()
            Timer2.Start()
        Else
            btncapture.Visible = True
            cam = New captureGIGI.Capture(VIDEODEVICE, 640, 480, VIDEOBITSPERPIXEL, PictureBox1)
        End If


        

    End Sub

    Private Sub kaptur()
        If Not m_ip.Equals(IntPtr.Zero) Then
            Marshal.FreeCoTaskMem(m_ip)
            m_ip = IntPtr.Zero
        End If

        ' capture image
        m_ip = cam.Click()
        Dim b As New Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip)

        ' If the image is upsidedown
        b.RotateFlip(RotateFlipType.RotateNoneFlipY)
        PictureBox2.Image = b
    End Sub

    Private Sub killapp(ByVal valtokill As String)
        Try
            'Get the selected item on the listbox
            Dim whole As String = valtokill
            'Get all the text after the last "#" in the item
            Dim pid = whole.Substring(whole.LastIndexOf(" # "))
            'Replace " # " with nothing, to leave the ID of the process alone
            pid = pid.Replace(" # ", "")
            'Get the process which has the same ID as the one selected in the listbox
            Dim p As Process = Process.GetProcessById(pid)
            Try
                'Try to kill the process (this may not work if you're not an Administrator, if the process has already ended, or if the process has been protected at kernel ring zero (maybe I'll do a tutorial on that later...)
                p.Kill()

                'Dim dirname As String
                'dirname=p.

                'p.Dispose()
            Catch ex As Exception
                'If we can't kill the process, show an error
                MessageBox.Show(ex.Message)
            End Try
        Catch ex As Exception
            'If we can't get the selected item of the listbox (most likely because the user has not selected it), then show an error.
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        listrefrsh()
        For Each str As String In lstMain.Items
            If str.StartsWith("vsnpstd3") Then
                killapp(str)
                kaptur()
                Exit Sub
            End If
        Next
    End Sub


    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

        Dim proc() As Process = System.Diagnostics.Process.GetProcessesByName("vsnpstd3")

        If proc.Length > 0 Then
            Exit Sub
        Else
            Process.Start("vsnpstd3")
        End If
    End Sub

    Private Sub listrefrsh()
        lstMain.Items.Clear()
        'For every process which is running on the computer
        For Each p As Process In Process.GetProcesses
            'If the MainWindowTitle of the process is not empty 
            If p.MainWindowTitle = String.Empty = False Then
                'Add the process name, the main window title, and the process ID (what windows uses to identify the process) to the listbox)
                lstMain.Items.Add(p.ProcessName & " # " & p.MainWindowTitle & " # " & p.Id)
            End If
        Next

    End Sub

    Public Overrides Sub SetSimpan()
        MyBase.SetSimpan()
        pic = PictureBox2.Image
        DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Public Overrides Sub SetReset()
        MyBase.SetReset()
        Me.Close()
    End Sub

    Private Sub btncapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncapture.Click
        Try
            kaptur()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Capture Information", MessageBoxButtons.OK, MessageBoxIcon.Warning)

        End Try

    End Sub
End Class